package com.ca.ridersclubapplication.view.card;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.ca.ridersclubapplication.request.FileRequester;
import com.ca.ridersclubapplication.request.dto.PaperCardDto;
import com.ca.ridersclubapplication.util.ImageUtils;
import com.ca.ridersclubapplication.view.button.IconButton;
import com.ca.ridersclubapplication.R;
import com.ca.ridersclubapplication.data.bean.MiniComment;
import com.ca.ridersclubapplication.data.bean.PaperInfo;
import com.ca.ridersclubapplication.util.ImageCacheUtils;
import com.ca.ridersclubapplication.util.PaperUtils;
import com.ca.ridersclubapplication.util.TimeUtils;

public class PaperCard extends ConstraintLayout {

    private PaperCardDto paperCardDto;

    public ImageView headerView;
    public TextView nameView;
    public ImageView downIcon;
    public TextView timeView;
    public ImageView carIcon;
    public TextView clubNameView;

    public TextView titleView;
    public PaperCardImageView imageView;
    public TextView contentView;
    public MiniCommentCard commentCard;

    public IconButton supportButton;
    public IconButton commentButton;
    public TextView moreView;

    @RequiresApi(api = Build.VERSION_CODES.O)
    public PaperCard(@NonNull Context context) {
        super(context);
        initView(context, null);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public PaperCard(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public PaperCard(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void initView(Context context, AttributeSet attrs) {
        View view = LayoutInflater.from(context).inflate(R.layout.paper_card_layout, this);

        headerView = view.findViewById(R.id.iv_header);
        downIcon = view.findViewById(R.id.icon_down);
        nameView = view.findViewById(R.id.tv_name);
        timeView = view.findViewById(R.id.tv_time);
        carIcon = view.findViewById(R.id.icon_car);
        clubNameView = view.findViewById(R.id.tv_clubName);

        titleView = view.findViewById(R.id.tv_title);
        imageView = view.findViewById(R.id.imageView);
        contentView = view.findViewById(R.id.tv_content);

        commentCard = view.findViewById(R.id.card_comment);

        supportButton = view.findViewById(R.id.ib_support);
        commentButton = view.findViewById(R.id.ib_comment);
        moreView = view.findViewById(R.id.tv_more);

        headerView.setOnClickListener(v -> {});
        clubNameView.setOnClickListener(v -> {});
        downIcon.setOnClickListener(v -> {});

        paperCardDto = new PaperCardDto();

        if (attrs != null) {
            TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PaperCard, 0, 0);

            paperCardDto.setAuthorName(array.getString(R.styleable.PaperCard_pc_name));
            paperCardDto.setTime(array.getString(R.styleable.PaperCard_pc_time));
            paperCardDto.setClubName(array.getString(R.styleable.PaperCard_pc_clubName));

            paperCardDto.setTitle(array.getString(R.styleable.PaperCard_pc_title));
            paperCardDto.setContent(array.getString(R.styleable.PaperCard_pc_content));

            paperCardDto.setSupportCount((long) array.getInteger(R.styleable.PaperCard_pc_supportCount, 0));
            paperCardDto.setCommentCount((long) array.getInteger(R.styleable.PaperCard_pc_commentCount, 0));

            array.recycle();
        }

        setOnClickListener(v -> {});
    }

    public PaperCardDto getPaper() {
        return paperCardDto;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public void setPaper(PaperCardDto dto) {
        if (dto == null) {
            dto = new PaperCardDto();
        }
        this.paperCardDto = dto;

        ImageUtils.loadCircleImage(getContext(), FileRequester.HEADER_BY_ID_BASE_URL + dto.getAuthorId(), headerView);
        nameView.setText(dto.getAuthorName());
        timeView.setText(TimeUtils.parseShowDateTime(dto.getTime()));
        clubNameView.setText(dto.getClubName());

        titleView.setText(dto.getTitle());

        //  解析内容，获取文章图片地址数组
        String[] array = PaperUtils.parsePaperImageUrlArray(dto.getContent());
        imageView.setImageUrlArray(array);

        //  解析XML文章内容
        contentView.setText(PaperUtils.parsePaperForText(dto.getContent()));

        MiniComment comment = null;
        if (dto.getCommentId() != 0L) {
            comment = new MiniComment(
                    dto.getCommentSupportCount(),
                    dto.getCommentAccountName(),
                    dto.getCommentContent());
        }
        commentCard.setComment(comment);

        supportButton.setValue(dto.getSupportCount());
        commentButton.setValue(dto.getCommentCount());
    }
}
